/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TablaCliente.java
 *
 * Created on 06-dic-2010, 13:08:09
 */
package proyectoempresa.igu;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import proyectoempresa.DAO.FacturaDao;
import proyectoempresa.modelo.Factura;
import proyectoempresa.modeloTabla.ModeloTablaFactura;

/**
 *
 * @author Diego
 */
public class TablaCliente extends javax.swing.JDialog {

    /** Creates new form TablaCliente */
    List<Factura> filas;
    List<Factura> filasas;
    public TablaCliente(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setTitle("Ferretria Fhersa");
       filas= new ArrayList<Factura>();
       filasas= new ArrayList<Factura>();
        FacturaDao fd= new FacturaDao(null);
        filasas=fd.buscarTodos();
        initComponents();
    
        txtbusqueda.setEditable(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public TablaCliente() {
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelImage1 = new org.edisoncor.gui.panel.PanelImage();
        buttonColoredAction1 = new org.edisoncor.gui.button.ButtonColoredAction();
        buttonColoredAction2 = new org.edisoncor.gui.button.ButtonColoredAction();
        buttonColoredAction3 = new org.edisoncor.gui.button.ButtonColoredAction();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ModeloTablaFactura modelo= new ModeloTablaFactura(filas);
        tabla = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        comboBusqueda = new javax.swing.JComboBox();
        txtbusqueda = new javax.swing.JTextField();
        buttonColoredAction4 = new org.edisoncor.gui.button.ButtonColoredAction();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Imagenes/librostart.jpg"))); // NOI18N

        buttonColoredAction1.setText("Editar");
        buttonColoredAction1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonColoredAction1ActionPerformed(evt);
            }
        });

        buttonColoredAction2.setText("Salir");
        buttonColoredAction2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonColoredAction2ActionPerformed(evt);
            }
        });

        buttonColoredAction3.setText("Agregar");
        buttonColoredAction3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonColoredAction3ActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 4, true), "LISTA DE FACTURAS", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        tabla.setModel(modelo);
        jScrollPane1.setViewportView(tabla);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel1.setText("Criterio de Busqueda:");

        comboBusqueda.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ">>>>>", "C_Registro", "Fecha" }));
        comboBusqueda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBusquedaActionPerformed(evt);
            }
        });

        buttonColoredAction4.setText("Buscar");
        buttonColoredAction4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonColoredAction4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelImage1Layout = new javax.swing.GroupLayout(panelImage1);
        panelImage1.setLayout(panelImage1Layout);
        panelImage1Layout.setHorizontalGroup(
            panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelImage1Layout.createSequentialGroup()
                .addGroup(panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelImage1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panelImage1Layout.createSequentialGroup()
                                .addComponent(buttonColoredAction3, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(buttonColoredAction1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(38, 38, 38)
                                .addComponent(buttonColoredAction2, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(panelImage1Layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(comboBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtbusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(44, 44, 44)
                        .addComponent(buttonColoredAction4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        panelImage1Layout.setVerticalGroup(
            panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelImage1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtbusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonColoredAction4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelImage1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonColoredAction1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonColoredAction2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonColoredAction3, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelImage1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panelImage1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonColoredAction3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonColoredAction3ActionPerformed
        // TODO add your handling code here:
        ModeloTablaFactura modelo = (ModeloTablaFactura) tabla.getModel();
        new Facturacion(new JFrame(), true, new Factura(), modelo, 1);
        modelo.fireTableStructureChanged();
        this.setVisible(false);
        getParent().setVisible(false);
         dispose();
    }//GEN-LAST:event_buttonColoredAction3ActionPerformed

    private void buttonColoredAction2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonColoredAction2ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_buttonColoredAction2ActionPerformed

    private void buttonColoredAction4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonColoredAction4ActionPerformed
        // TODO add your handling code here:
     int a = comboBusqueda.getSelectedIndex();
            FacturaDao fd = new FacturaDao(null);
         ModeloTablaFactura modelo = (ModeloTablaFactura) tabla.getModel();
         List<Factura> listad= fd.buscarTodos();
         boolean as=false;
         Factura encontado=null;
         if (a==1) {
            for (Factura factura : listad) {
                // JOptionPane.showMessageDialog(null,factura.getId_cliente()+""+(txtbusqueda.getText().trim()));
             if (Integer.parseInt(""+factura.getId_cliente()) == Integer.parseInt(txtbusqueda.getText().trim()) ){
                    as=true;
                    encontado=factura;
             }
        }if (as) {
           // JOptionPane.showMessageDialog(null,encontado);
        filas.clear();
       filas.add(encontado);
        modelo.setFilas(filas);
        modelo.fireTableStructureChanged();
        modelo.fireTableDataChanged();
        }else{
        JOptionPane.showMessageDialog(null," Id del Cliente no Encontrado");
        filas.clear();
         modelo.fireTableStructureChanged();
        modelo.fireTableDataChanged();

        }

        }else if(a==2){
            List<Factura>todos=new ArrayList<Factura>();
        for (Factura factura : listad) {
              //   JOptionPane.showMessageDialog(null,factura.getFecha()+":"+(txtbusqueda.getText().trim()));
             if (factura.getFecha().equals(txtbusqueda.getText().trim()) ){
                    as=true;
//                    JOptionPane.showMessageDialog(null,factura);
                    todos.add(factura);

             }
        }if (as) {
           // JOptionPane.showMessageDialog(null,encontado);
        filas.clear();
       filas.addAll(todos);
        modelo.setFilas(filas);
        modelo.fireTableStructureChanged();
        modelo.fireTableDataChanged();
        }else{
        JOptionPane.showMessageDialog(null," No se ha Encontrado nada por cedula");
        filas.clear();
         modelo.fireTableStructureChanged();
        modelo.fireTableDataChanged();

        }
        }
         
    }//GEN-LAST:event_buttonColoredAction4ActionPerformed

    private void buttonColoredAction1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonColoredAction1ActionPerformed
        // TODO add your handling code here:
        if (tabla.getSelectedRow()==-1){
            JOptionPane.showMessageDialog(null, "Debe seleccionar un Registro");
        }else{
        ModeloTablaFactura modelo = (ModeloTablaFactura) tabla.getModel();
        new Facturacion(new JFrame(), true, modelo.getFactura(), modelo, 2);
        modelo.fireTableStructureChanged();
        dispose();
        }
    }//GEN-LAST:event_buttonColoredAction1ActionPerformed

    private void comboBusquedaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBusquedaActionPerformed
        // TODO add your handling code here:
        if (comboBusqueda.getSelectedIndex() != -1) {
            txtbusqueda.setEditable(true);
            System.out.println(comboBusqueda.getSelectedIndex());
            txtbusqueda.setText("");
        }
}//GEN-LAST:event_comboBusquedaActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                TablaCliente dialog = new TablaCliente(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.edisoncor.gui.button.ButtonColoredAction buttonColoredAction1;
    private org.edisoncor.gui.button.ButtonColoredAction buttonColoredAction2;
    private org.edisoncor.gui.button.ButtonColoredAction buttonColoredAction3;
    private org.edisoncor.gui.button.ButtonColoredAction buttonColoredAction4;
    private javax.swing.JComboBox comboBusqueda;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.edisoncor.gui.panel.PanelImage panelImage1;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField txtbusqueda;
    // End of variables declaration//GEN-END:variables

    public JTable getTabla() {
        return tabla;
    }

    public void setTabla(JTable tabla) {
        this.tabla = tabla;
    }
}
